/*
 * 评价记录
 * page评价记录
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/msg/assessRecordsBolc.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/assessModel.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:qgchat/widgets/noData.dart';
import 'package:qgchat/widgets/avatar.dart';

class Assess extends StatefulWidget {
  Assess({Key key}) : super(key: key);

  @override
  _AssessState createState() => _AssessState();
}

class _AssessState extends State<Assess> {
  String token = '';

  // 生成appbar
  Widget appbarHandle() {
    return PreferredSize(
      preferredSize: Size.fromHeight(px(88)),
      child: AppBar(
        centerTitle: true,
        title: Text(
          "评价",
          style: TextStyle(
            color: Colors.black,
            fontSize: px(34),
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    token = state(context, 'user').info.memberToken;
    return Scaffold(
      appBar: appbarHandle(),
      body: BlocProvider(
        bloc: AssessRecordsBlog(token),
        child: BodyHandle(),
      ),
    );
  }
}

class BodyHandle extends StatelessWidget {
  // 生成主播列表
  Widget makeAnchorHandle(AssessModel item) {
    List<Widget> itemTagList() {
      return item.tagLabel.map((tagItem) {
        return Container(
          height: px(46),
          alignment: Alignment.center,
          margin: EdgeInsets.only(right: px(15)),
          padding: EdgeInsets.symmetric(horizontal: px(15)),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(px(23)),
            border: Border.all(
              color: col("edeff2"),
              width: 1,
            ),
          ),
          child: Text(
            tagItem,
            style: TextStyle(
              color: col("303133"),
              fontSize: px(24),
            ),
          ),
        );
      }).toList();
    }

    return Container(
      height: px(235),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: px(30)),
            child: Avatar(
              item.memberHead,
              true,
              token: item.memberToken,
            ),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: px(23)),
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    color: col("edeff2"),
                    width: 1,
                  ),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  // 昵称和时间
                  Container(
                    margin: EdgeInsets.only(top: px(38)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          item.memberNickname,
                          style: TextStyle(
                            height: .95,
                            color: col("303133"),
                            fontSize: px(34),
                          ),
                        ),
                        Text(
                          "${Date.simple(int.parse(item.createtime), format: "y-m-d h:i")}",
                          style: TextStyle(
                            height: .99,
                            color: col("bfc2cc"),
                            fontSize: px(26),
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 评价title
                  Container(
                    margin: EdgeInsets.only(top: px(20)),
                    child: Text(
                      "给我的评价",
                      style: TextStyle(
                        color: col("909399"),
                        fontSize: px(28),
                      ),
                    ),
                  ),
                  // 给我的评价
                  Container(
                    margin: EdgeInsets.only(top: px(25)),
                    child: Row(
                      children: itemTagList(),
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  List<Widget> assessList(List<AssessModel> list) {
    return list.map((item) {
      return makeAnchorHandle(item);
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    AssessRecordsBlog blog = BlocProvider.of<AssessRecordsBlog>(context);
    return StreamBuilder(
      stream: blog.assessRecordsStream,
      initialData: blog.defaultData,
      builder: (
        BuildContext context,
        AsyncSnapshot snapshot,
      ) {
        List<AssessModel> list = (snapshot.data as List<AssessModel>).cast();
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else {
          if (list.isEmpty) {
            return NoData("还没有任何我的评价相关消息");
          } else {
            return Container(
              color: Colors.white,
              child: EasyRefresh(
                header: EasyRefreshSetting.header,
                footer: EasyRefreshSetting.footer,
                onRefresh: () async {
                  blog.onRefresh();
                },
                onLoad: () async {
                  blog.getAssessRecordsLists();
                },
                child: ListView(
                  children: assessList(list),
                ),
              ),
            );
          }
        }
      },
    );
  }
}
